package homework2.server;

import homework2.common.CommandType;
import homework2.common.Message;
import homework2.utils.Tool;

import java.io.*;
import java.net.Socket;

/**
 * 该类的对象与客户端保持一一通信
 *
 * @author jian
 * @date 2024/01/08 19:29
 **/
public class ServerConnectClientThread extends Thread {


    private Socket socket;
    private String id;//连接到服务端的用户id

    public ServerConnectClientThread(Socket socket, String id) {
        this.socket = socket;
        this.id = id;
    }


    @Override
    public void run() {
        try {
            while (true){
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();//读取来自客户端的消息，如果读取不到，则阻塞
                switch (message.getCommandType()){
                    case CommandType.USER_LIST:
                        getUserList();
                        break;
                    case CommandType.TARGET:
                        handleSendTarget(message);
                        break;
                    case CommandType.SHOW:
                        getFile(message);
                        break;
                    case CommandType.ALL:
                        handleSendAll(message);
                        break;
                    default:
                        System.out.println("没有此选型请重新输入命令");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    private void getFile(Message message) throws Exception {
        String filePath = message.getContent();
        if (Tool.fileIsExist(filePath)){
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            File file = new File(filePath);
            try (FileInputStream fileInputStream = new FileInputStream(file)) {
                byte[] fileData = new byte[(int) file.length()];
                fileInputStream.read(fileData);
                // 加密文件数据
                byte[] encryptedData = Tool.encryptFile(fileData);

                message.setFileBytes(encryptedData);

                // 发送Message对象
                objectOutputStream.writeObject(message);
            }
        }
    }

    private void handleSendAll(Message message) throws IOException {
        message.setUserId(id);
        for (ServerConnectClientThread serverConnectClientThread : ManageClientThreads.getHm().values()) {
            if (!serverConnectClientThread.returnId().equals(id)){
                ObjectOutputStream oos =
                        new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                message.setGetter(serverConnectClientThread.returnId());
                oos.writeObject(message);
            }
        }
    }
    private void handleSendTarget(Message message) throws IOException {
        String target = message.getGetter();
        message.setUserId(id);
        message.setGetter(target);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientThreads.getServerConnectClientThread(target).getSocket().getOutputStream());
        objectOutputStream.writeObject(message);

    }

    public void getUserList() throws IOException {
        String onlineUser = ManageClientThreads.getOnlineUser();
        //返回message
        //构建一个Message 对象，返回给客户端
        Message message2 = new Message();
        message2.setCommandType(CommandType.USER_LIST);
        message2.setContent(onlineUser);
        //返回给客户端
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(message2);
    }

    public Socket getSocket(){
        return socket;
    }
    
    public String returnId(){
        return id;
    }

}
